class Solution {
    bool checkCol[10],checkDig1[20],checkDig2[20];
    vector<vector<string>> ret;
    vector<string> path;
    int n;
public:
    vector<vector<string>> solveNQueens(int _n) {
        n=_n;
        path.resize(n);
        for(int i=0;i<n;i++)
            path[i].append(n,'.');
        dfs(0);
        return ret;
    }
    void dfs(int row)
    {
        if(row==n)
        {
            ret.push_back(path);
            return;
        }
        for(int col=0;col<n;col++)
        {
            if(!checkCol[col] && !checkDig1[row-col+n] && !checkDig2[row+col])
            {
                path[row][col]='Q';
                checkCol[col]=checkDig1[row-col+n]=checkDig2[row+col]=true;
                dfs(row+1);
                path[row][col]='.';
                checkCol[col]=checkDig1[row-col+n]=checkDig2[row+col]=false;
            }
        }
    }
};
